package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import contacts614.po.Room;
import dao.IRoomDao;
import db.DbConnection;

public class RoomImpl implements IRoomDao {
	// 宿舍要单独管理，先有宿舍号才能加入
	/**
	 * 添加宿舍信息
	 */
	@Override
	public void addRoom(Room room) {
		Connection conn = DbConnection.getConnection();
		String roomNo = room.getRoomNo();
		String roomName = room.getRoomName();
		String sql = "insert into room values(?,?)";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, roomNo);
			ps.setString(2, roomName);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 修改宿舍信息
	 */
	@Override
	public void updateRoom(Room room) {
		Connection conn = DbConnection.getConnection();
		String roomNo = room.getRoomNo();
		String roomName = room.getRoomName();
		String sql = "update room set roomName=? where roomNo=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, roomName);
			ps.setString(2, roomNo);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}

	/**
	 * 查找宿舍信息
	 */
	@Override
	public Room findOneRoom(String roomId) {// 不是roomNo?
		// TODO Auto-generated method stub
		Connection conn = DbConnection.getConnection();
		List<Room> roomList = new ArrayList<Room>();
		String sql = "select roomNo,roomName where roomNo=?";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, roomId);
			rs = ps.executeQuery();
			while (rs.next()) {
				String roomNo = rs.getString(1);
				String roomName = rs.getString(2);
				Room room = new Room(roomNo, roomName);
				roomList.add(room);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, rs);
		}
		return roomList.get(0);// 返回第一个宿舍号，集合索引为0
	}

	/**
	 * 展示宿舍信息
	 */
	@Override
	public List<Room> findAllRoom() {
		// TODO Auto-generated method stub
		Connection conn = DbConnection.getConnection();
		List<Room> roomList = new ArrayList<Room>();
		String sql = "select roomNo,roomName from room";
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				String roomNo = rs.getString(1);
				String roomName = rs.getString(2);
				Room room = new Room(roomNo, roomName);
				roomList.add(room);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, rs);
		}
		return roomList;
	}

	/**
	 * 删除宿舍信息
	 */
	@Override
	public void deleteRoom(String roomId) {
		// TODO Auto-generated method stub
		Connection conn = DbConnection.getConnection();
		String sql = "delete from room where roomNo=?";
		PreparedStatement ps = null;
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, roomId);
			ps.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			DbConnection.close(ps, null);
		}
	}
}
